<?php
include("controllers/read.php");
$db = openDB("database.sqlite");
$config = getConfig($db);

/******* page edits **********/

if(!$_POST['edit_body'] == "") {
  // get latest id's
  $latestid = getLatest($db);
  // get requested title
  $title = $_GET['title'];
  // remember author
  setcookie("name",$_POST['edit_author'],time()+60*60*24*30);
  // get links
  $links = array();
  $split = split("}",$_POST['edit_body']);
  for($i=0; $i<count($split)-1; $i++) {
    $segment = $split[$i];
    $split2 = split("{",$segment);
    $linktarget = $split2[1];
    array_push($links,$linktarget);
  }
  // check if page already exists
  if(!exists($db,"pages","title",$title)) {
    // NEW PAGE
    // add to pages table
    $id = $latestid['pages']+1;
    // add to pages table
    $query = "INSERT INTO pages (
      id,
      title,
      tags
    ) VALUES (
      $id,
      '$title',
      '".sqlite_escape_string(stripslashes($_POST['edit_tags']))."'
    )";
    sqlite_query($db,$query);
  } else {
    // EDIT/REVISION
    // find the page's id
    $id = sqlite_fetch_single(sqlite_query($db,"SELECT id FROM pages WHERE title='$title'"));
    // update tags
    sqlite_query($db,"UPDATE pages SET tags='".$_POST['edit_tags']."' WHERE title LIKE '$title'");
  }
  // write the new revision
  sqlite_query($db,"INSERT INTO revisions (
    author,
    ip,
    pageid,
    time,
    body
  ) VALUES (
    '".$_POST['edit_author']."',
    '".$_SERVER['REMOTE_ADDR']."',
    $id,
    ".time().",
    '".sqlite_escape_string(stripslashes($_POST['edit_body']))."'
  )");
  // delete existing links from this page
  sqlite_query($db,"DELETE FROM links WHERE fromid = $id");
  // write links
  foreach($links as $link) {
    sqlite_query($db,"INSERT INTO links (
      fromid,
      to
    ) VALUES (
      $id,
      '$link'
    )");
  }
  // parse, write tags
  $tags = split(" ",$_POST['edit_tags']);
  foreach ($tags as $tag) {
    if(!exists($db,"tags","name",$tag)) {
      sqlite_query($db,"INSERT INTO tags (
        id,
        name,
        parent
      ) VALUES (
        ".($latestid['tags']+1).",
        '".$tag."',
        'none'
      )");
    }
  }
  // go back to the page
  header("location: ".$config['url']."?show=$title");
} else if(!$_POST['config_name'] == "") {
  
  /***** config *******/
  
  if(!$_POST['config_url'] == "") {
    sqlite_query($db,"UPDATE config SET url = '".sqlite_escape_string(stripslashes($_POST['config_url']))."' WHERE url LIKE 'http://%'");
  }
  if(!$_POST['config_name'] == "") {
    sqlite_query($db,"UPDATE config SET name = '".sqlite_escape_string(stripslashes($_POST['config_name']))."' WHERE url LIKE 'http://%'");
  }
  if(!$_POST['config_tagline'] == "") {
    sqlite_query($db,"UPDATE config SET tagline = '".sqlite_escape_string(stripslashes($_POST['config_tagline']))."' WHERE url LIKE 'http://%'");
  }
  if(!$_POST['config_skin'] == "") {
    sqlite_query($db,"UPDATE config SET skin = '".sqlite_escape_string(stripslashes($_POST['config_skin']))."' WHERE url LIKE 'http://%'");
  }
  header("location: ".$_POST['config_url']);
} else {
  
  /****** tag ********/
  
  sqlite_query($db,"UPDATE tags SET parent='".$_POST['tag_parent']."' WHERE name='".$_GET['tag']."'");
  header("location: ".$config['url']."?tag=".$_GET['tag']);
  
}
?>